﻿using System;
using System.Collections.Generic;
using UnityEngine;
using Yoozoo.Core.Extensions.CSharp;

namespace Yoozoo.Modules.Furnitures
{
    /// <summary>
    /// 平面
    /// 通过平面上一点（原点）和平面的法线来确定平面 （点法式）
    /// </summary>
    [Serializable]
    public class Plane
    {
        /// <summary>
        /// 原点坐标
        /// </summary>
        public Vector3 OriginCoordinate
        {
            get
            {
                if (OriginPoint)
                {
                    return OriginPoint.position;
                }
                return Vector3.zero;
            }
        }

        /// <summary>
        /// 法线向量
        /// </summary>
        public Vector3 Normal
        {
            get
            {
                if (OriginPoint)
                {
                    return OriginPoint.up;
                }
                return Vector3.up;
            }
        }

        /// <summary>
        /// X轴方向
        /// </summary>
        public Vector3 AxisX
        {
            get
            {
                if (OriginPoint)
                {
                    return OriginPoint.right;
                }
                return Vector3.right;
            }
        }

        /// <summary>
        /// Y轴方向
        /// </summary>
        public Vector3 AxisY
        {
            get
            {
                if (OriginPoint)
                {
                    return OriginPoint.forward;
                }
                return Vector3.forward;
            }
        }

        public UnityEngine.Plane GetPlane()
        {
            return new UnityEngine.Plane(Normal, OriginCoordinate);
        }

        /// <summary>
        /// 平面上的坐标系原点，并且
        /// x轴是坐标系x轴
        /// y轴是平面的法线
        /// z轴是坐标系的y轴
        /// </summary>
        public Transform OriginPoint;

        /// <summary>
        /// 长（x）
        /// </summary>
        public int Length;

        /// <summary>
        /// 宽（y）
        /// </summary>
        public int Width;

        [Tooltip("有效区域多边形(世界坐标)")]
        public List<Vector3> Polygon = new List<Vector3>();

        public PlaneType PlaneType = PlaneType.Wall;

        public void AddPoint(Vector3 pointInWorld)
        {
            //var pointInPlane = OriginPoint.InverseTransformPoint(pointInWorld);
            Polygon.Add(pointInWorld);
        }

        public Vector3 GetPointInWorld(int index)
        {
            if (Polygon.IsEmpty())
            {
                return Vector3.zero;
            }

            if (index < 0 || index >= Polygon.Count)
            {
                return Vector3.zero;
            }

            var pointInPlane = Polygon[index];
            return pointInPlane; //OriginPoint.TransformPoint(pointInPlane);
        }

        /// <summary>
        /// 翻转平面
        /// </summary>
        public void Flip()
        {
            // 翻转平面的y轴朝向, 原点从p0移动到p1

            //       ↗ y轴
            //     ↗
            //   ↗
            //  p0 ---- p1 →→→ x轴
            //  |       |
            //  |       |
            //  |       | 
            //  |       |
            //  |       |
            //  p3 ---- p2
            //  ↓
            //  ↓
            //  ↓ z轴

            // x轴
            // ←←← p0 ---- p1
            //       |     ↙ |
            //       |   ↙   |
            //       | ↙     | 
            //       | y轴    |
            //       |        |
            //       p3 ---- p2
            //                ↓
            //                ↓
            //                ↓ z轴

            var p1 = OriginCoordinate + AxisX * Length;

            OriginPoint.position = p1;
            var z = AxisY;
            var y = -Normal;

            var rotation = Quaternion.LookRotation(z, y);
            OriginPoint.rotation = rotation;
        }
    }
}
